home *** CD-ROM | disk | FTP | other *** search
Wrap
/* $Id: COMImp.cpp 1.6 1997/05/12 19:06:20 damien Exp $ */ // Copyright (c) 1990-1995 Ray Dream, Inc. All rights reserved. // This file is an example of importer. // it's a simple importer of facets from a text file. // // the file is a set of surfaces designed as follow : // number of points // 1st point, x coordinate, y coordinate and z coordinate, // 2nd point, id // etc... // last point // // for example, a square is : // 4 // 0 0 0 // 0 1 0 // 1 1 0 // 1 0 0 // #ifndef __COMIMP__ #include "COMIMP.h" #endif #ifndef __3DCOFAIL__ #include "3DCoFail.h" #endif #ifndef __I3DSHUTI__ #include "I3DShUti.h" #endif #ifndef __I3DSHTRE__ #include "I3DShTre.h" #endif #ifndef __I3DSHOBJ__ #include "I3DShObj.h" #endif #ifndef __I3DSHSCN__ #include "I3DShScn.h" #endif #ifndef __IMPDLL__ #include "IMPDll.h" #endif #include <stdio.h> TEasyImporter::TEasyImporter() { fCRef=0; // Reference counter global_count_Obj++; } TEasyImporter::~TEasyImporter() { global_count_Obj--; } //IUnknown Interface : HRESULT TEasyImporter::QueryInterface(THIS_ REFIID riid, LPVOID* ppvObj) { *ppvObj=NULL; // The TDXFImporter knows the interfaces of the parent Objects if (IsEqualIID(riid, IID_IUnknown)) *ppvObj=(LPVOID)this; else if (IsEqualIID(riid, IID_I3DExImportFilter)) *ppvObj=(LPVOID)this; else if (IsEqualIID(riid, IID_I3DExDataExchanger)) *ppvObj=(LPVOID)this; else if (IsEqualIID(riid, IID_I3DExtension)) *ppvObj=(LPVOID)this; // we must add reference if we return an interface if (*ppvObj!=NULL) { ((LPUNKNOWN)*ppvObj)->AddRef(); return NOERROR; } else { return ResultFromScode(E_NOINTERFACE); } } ULONG TEasyImporter::AddRef(THIS) { return fCRef++; } ULONG TEasyImporter::Release(THIS) { if (fCRef==1) delete this; // last reference released, so object destroyed return fCRef--; } // I3DExtension methods : I3DExtension* TEasyImporter::Clone(THIS) { TEasyImporter* theClone = new TEasyImporter; if (theClone) { theClone->AddRef(); theClone->fData=fData; } return theClone; } HRESULT TEasyImporter::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) { InitCoFailure(shellUtilities); return NOERROR; } // I3DExDataExchanger methods : ExtensionDataMap* TEasyImporter::GetExtensionDataMap(THIS) { return NULL; } void* TEasyImporter::GetExtensionDataBuffer(THIS) { return &fData; // used by the shell to set the new parameters } HRESULT TEasyImporter::ExtensionDataChanged(THIS) { return NOERROR; } HRESULT TEasyImporter::HandleEvent(THIS_ ULONG SourceID) { return ResultFromScode(E_NOTIMPL); } short TEasyImporter::GetResID(THIS) { return 128; // this is the view ID in the resource file. } void TEasyImporter::StartProgress(IShFileStream* afile) { fProgressMax=afile->GetFileSize(); //if (fData.fIsSmooth) fProgressMax = fProgressMax*2; gShellUtilities->ProgressStart("Importing ESY", fProgressMax); } // I3DExImportFilter methods : BOOLEAN TEasyImporter::Prepare(char* /*fullPathName*/, I3DShScene* /*scene*/, I3DShTreeElement* /*fatherTree*/) { return TRUE; } BOOLEAN TEasyImporter::WantsOptionDialog() { return FALSE; } HRESULT TEasyImporter::DoImport(THIS_ char* fullPathName, I3DShScene* scene, I3DShTreeElement* fatherTree) { I3DShTreeElement* topTree; FailInfo fi; DECLAREVOLATILE(IShFileStream*, stream = NULL); if (TRY(fi)) { gShellUtilities->CoCreateInstance(CLSID_StandardFileStream, NULL, CLSCTX_INPROC_SERVER, IID_IShFileStream, (LPVOID*) &stream); FailNIL(stream); FailOSErr((short)stream->InitFileStream(fullPathName, kShStreamIn)); StartProgress(stream); if (fatherTree == NULL) { I3DShGroup* topGroup; scene->CreateTreeRootIfNone(); topGroup = scene->GetTreeRoot(); topGroup->QueryInterface(IID_I3DShTreeElement, (LPVOID*) &topTree); topGroup->Release(); } else { gShellUtilities->CoCreateInstance(CLSID_StandardGroup, NULL, CLSCTX_INPROC_SERVER, IID_I3DShTreeElement, (LPVOID*) &topTree); topTree->SetScene(scene); fatherTree->InsertLast(topTree); } scene->CreateRenderingCameraIfNone(IDTYPE('c', 'o', 'n', 'i'), (fatherTree == NULL)); // Create a onical rendering camera if none, and a Distant light if we not importing in an existing scene DoReadEasyFile(stream, scene, topTree); stream->Release(); stream = nil; gShellUtilities->ProgressDone(); fi.Success(); } else { if (stream) stream->Release(); gShellUtilities->ProgressDone(); fi.ReSignal(); } return NOERROR; } BOOLEAN TEasyImporter::WantsTopScene () { return FALSE; } /* load an integer from a stream HRESULT ReadIntStream(IShFileStream* stream, int* res) { char c; HRESULT error; *res=0; if ((error=stream->Get(&c)) != NOERROR) return error; while ((c==' ') || (c==13) || (c==10) || (c==9)) if ((error=stream->Get(&c)) != NOERROR) return error; if ((c<'0') || (c>'9')) return ERROR_INVALID_BLOCK; while ((c>='0') && (c<='9')) { *res=*res*10+(c-'0'); if ((error=stream->Get(&c)) != NOERROR) if (error==ERROR_HANDLE_EOF) c=0; else return error; } return NOERROR; }*/ // load a 3d vector from a stream HRESULT ReadStreamVector3D(IShFileStream* stream, VECTOR3D* res) { Double d; HRESULT error; if ((error=stream->GetDouble(&d)) != NOERROR) return error; (*res)[0]=d; if ((error=stream->GetDouble(&d)) != NOERROR) return error; (*res)[1]=d; if ((error=stream->GetDouble(&d)) != NOERROR) return error; (*res)[2]=d; return NOERROR; } FACET3D* SetEasyFacet(VECTOR3D pA,VECTOR3D pB,VECTOR3D pC) { //VECTOR3D vAB, vAC, vBC; FACET3D* facet=new FACET3D; /*vAB=pB-pA; vAC=pC-pA; vBC=pC-pB; /*facet->fVertices[0].fNormal=vAB ^ vAC; facet->fVertices[1].fNormal=vAB ^ vBC; facet->fVertices[2].fNormal=vAC ^ vBC;*/ facet->fVertices[0].fVertex[0]=pA[0]; facet->fVertices[0].fVertex[1]=pA[1]; facet->fVertices[0].fVertex[2]=pA[2]; facet->fVertices[1].fVertex[0]=pB[0]; facet->fVertices[1].fVertex[1]=pB[1]; facet->fVertices[1].fVertex[2]=pB[2]; facet->fVertices[2].fVertex[0]=pC[0]; facet->fVertices[2].fVertex[1]=pC[1]; facet->fVertices[2].fVertex[2]=pC[2]; /*facet->fVertices[0].fNormal.Normalize(); facet->fVertices[1].fNormal.Normalize(); facet->fVertices[2].fNormal.Normalize();*/ facet->fReserved=0; return facet; } void TEasyImporter::DoReadEasyFile(IShFileStream* stream, I3DShScene* scene, I3DShTreeElement* topTree) { long nbPoints; int i; I3DShObject* object = nil; VOLATILE(object); VECTOR3D firstPoint; VECTOR3D secondPoint; VECTOR3D lastPoint; I3DShPolygonList* surface; I3DShInstance* instance = nil; VOLATILE(instance); I3DShTreeElement* instanceTree; char objName[10]; short counter=0; FailOSErr(stream->GetLong(&nbPoints)); while (nbPoints) { gShellUtilities->CoCreateInstance(CLSID_StandardPolygonList, NULL, CLSCTX_INPROC_SERVER, IID_I3DShPolygonList, (LPVOID*) &surface); surface->Init(FALSE /*no normals*/, FALSE /*no UV space*/); surface->PreAllocateFacets(nbPoints - 2/*nb of facets*/); FailOSErr(ReadStreamVector3D(stream, &firstPoint)); FailOSErr(ReadStreamVector3D(stream, &lastPoint)); for (i=2; i<nbPoints; i++) { secondPoint[0]=lastPoint[0]; secondPoint[1]=lastPoint[1]; secondPoint[2]=lastPoint[2]; FailOSErr(ReadStreamVector3D(stream, &lastPoint)); // add a facet surface->AddFacet (SetEasyFacet(firstPoint, secondPoint, lastPoint)); } surface->CalcNormals(30.0); // 30 degrees surface->QueryInterface(IID_I3DShObject, (LPVOID*) &object); surface->Release(); COLOR3D defColor; defColor.Mode=0; defColor.R=1.0; defColor.G=0.0; defColor.B=0.0; defColor.A=1.0; object->SetSimpleShading(&defColor, 1.0, 1.0, 0.0, 0.0); sprintf(objName,"Easy %i",++counter); object->SetName(objName); gShellUtilities->CoCreateInstance(CLSID_StandardInstance, NULL, CLSCTX_INPROC_SERVER, IID_I3DShInstance, (LPVOID*) &instance); instance->Set3DObject(object); instance->QueryInterface(IID_I3DShTreeElement, (LPVOID*) &instanceTree); instanceTree->SetScene(scene); instanceTree->CenterHotPointOnElement(); instance->Release(); topTree->InsertLast(instanceTree); FailOSErr(stream->GetLong(&nbPoints)); } }